package com.ruoyi.common.utils;

public class BaiduToWgs84 {

    public double[] FromXY(double xIn, double yIn) {
        double[] xy = new double[2];
        double[] gcjPoint = this.bd_decrypt(yIn, xIn);
        double[] wgs84Point = this.gcj_decrypt(gcjPoint[1], gcjPoint[0]);
        xy[0] = wgs84Point[0];
        xy[1] = wgs84Point[1];
        return xy;
    }

    private double[] bd_decrypt(double bdLat, double bdLon) {
        double[] xy = new double[2];
        double x = bdLon - 0.0065;
        double y = bdLat - 0.006;
        double z = Math.sqrt((x * x) + (y * y)) - (2E-05 * Math.sin(y * 52.359877559829883));
        double theta = Math.atan2(y, x) - (3E-06 * Math.cos(x * 52.359877559829883));
        xy[0] = z * Math.cos(theta);
        xy[1] = z * Math.sin(theta);
        return xy;
    }

    private double[] gcj_decrypt(double gcjLat, double gcjLon) {
        double[] xy = new double[2];
        if (this.outOfChina(gcjLat, gcjLon)) {
            xy[0] = gcjLon;
            xy[1] = gcjLat;
            return xy;
        }
        xy = this.delta(gcjLat, gcjLon);
        double[] xy_r = new double[2];
        xy_r[0] = gcjLon - xy[0];
        xy_r[1] = gcjLat - xy[1];
        return xy_r;
    }

    private double[] delta(double lat, double lon) {
        double[] xy = new double[2];
        double a = 6378245.0;
        double ee = 0.0066934216229659433;
        double dLat = this.transformLat(lon - 105.0, lat - 35.0);
        double dLon = this.transformLon(lon - 105.0, lat - 35.0);
        double radLat = (lat / 180.0) * 3.1415926535897931;
        double magic = Math.sin(radLat);
        magic = 1.0 - ((ee * magic) * magic);
        double sqrtMagic = Math.sqrt(magic);

        xy[0] = (dLon * 180.0) / (((a / sqrtMagic) * Math.cos(radLat)) * 3.1415926535897931);
        xy[1] = (dLat * 180.0) / (((a * (1.0 - ee)) / (magic * sqrtMagic)) * 3.1415926535897931);
        return xy;
    }

    private double transformLat(double x, double y) {
        double ret = ((((-100.0 + (2.0 * x)) + (3.0 * y)) + ((0.2 * y) * y)) + ((0.1 * x) * y)) + (0.2 * Math.sqrt(Math.abs(x)));
        ret += (((20.0 * Math.sin((6.0 * x) * 3.1415926535897931)) + (20.0 * Math.sin((2.0 * x) * 3.1415926535897931))) * 2.0) / 3.0;
        ret += (((20.0 * Math.sin(y * 3.1415926535897931)) + (40.0 * Math.sin((y / 3.0) * 3.1415926535897931))) * 2.0) / 3.0;
        return (ret + ((((160.0 * Math.sin((y / 12.0) * 3.1415926535897931)) + (320.0 * Math.sin((y * 3.1415926535897931) / 30.0))) * 2.0) / 3.0));
    }

    private double transformLon(double x, double y) {
        double ret = ((((300.0 + x) + (2.0 * y)) + ((0.1 * x) * x)) + ((0.1 * x) * y)) + (0.1 * Math.sqrt(Math.abs(x)));
        ret += (((20.0 * Math.sin((6.0 * x) * 3.1415926535897931)) + (20.0 * Math.sin((2.0 * x) * 3.1415926535897931))) * 2.0) / 3.0;
        ret += (((20.0 * Math.sin(x * 3.1415926535897931)) + (40.0 * Math.sin((x / 3.0) * 3.1415926535897931))) * 2.0) / 3.0;
        return (ret + ((((150.0 * Math.sin((x / 12.0) * 3.1415926535897931)) + (300.0 * Math.sin((x / 30.0) * 3.1415926535897931))) * 2.0) / 3.0));
    }

    private Boolean outOfChina(double lat, double lon) {
        if (((lon >= 72.004) && (lon <= 137.8347)) && ((lat >= 0.8293) && (lat <= 55.8271))) {
            return false;
        }
        return true;
    }
}
